import 'package:flutter/services.dart';
import 'package:xsdk_flutter/xsdk/xsdk_flutter_package.dart';
import 'package:xsdk_flutter_plugin/plugin/bean/x_method_command.dart';
import 'package:xsdk_flutter_plugin/plugin/x_utils_flutter_plugin.dart';
import 'base/xsdk_platform_interface.dart';


/*
内部开发使用

在这里实现各个功能,给对外工具类调用
 */
class XFlutterPluginMethodChannel extends XFlutterPluginPlatform {

  final methodChannel = const MethodChannel('xsdk_flutter_plugin');

  @override
  Future<String?> getPlatformVersion() async {
    final version = await methodChannel.invokeMethod<String>('getPlatformVersion');
    return version;
  }


  ////////////////////////////////////////////////////
  /*
  1.flutter设置接收原生通知回调(null为取消)
  注意:在新界面设置接收器,这会替换旧界面的接收器,返回旧界面时要重新设置
   */
  void setReceiveNativeMessageCallback(XFunctionResultCallback? callback){
    //设置接收原生通知消息
    XFlutterUtils.receiveNativeMessageCallback(methodChannel,callback);
  }

  //Android ////////////////////////////////////////////////////////////////////

  //支付宝碰一碰设备,初始化
  Future<dynamic> initAlipayIotSDK(String json) async{
    XMethodCommand methodCommand=XMethodCommand('init',json);
    final result = await methodChannel.invokeMethod<String>('XAlipayIotSDK',XJsonUtils.obj2Json(methodCommand));
    return result;
  }
  //支付宝碰一碰设备,发起普通支付,显示金额
  Future<dynamic> commonPayAlipayIotSDK(String json) async{
    XMethodCommand methodCommand=XMethodCommand('commonPay',json);
    final result = await methodChannel.invokeMethod<String>('XAlipayIotSDK',XJsonUtils.obj2Json(methodCommand));
    return result;
  }
  //支付宝碰一碰设备,发起会员支付
  Future<dynamic> memberPayAlipayIotSDK(String json) async{
    XMethodCommand methodCommand=XMethodCommand('memberPay',json);
    final result = await methodChannel.invokeMethod<String>('XAlipayIotSDK',XJsonUtils.obj2Json(methodCommand));
    return result;
  }
  //支付宝碰一碰设备,发起会员开卡
  Future<dynamic> memberCardAlipayIotSDK(String json) async{
    XMethodCommand methodCommand=XMethodCommand('memberCard',json);
    final result = await methodChannel.invokeMethod<String>('XAlipayIotSDK',XJsonUtils.obj2Json(methodCommand));
    return result;
  }


  //打开第二屏幕 分屏
  Future<dynamic> openSecondScreen(String webUrl) async{
    XMethodCommand methodCommand=XMethodCommand('openSecondScreen',webUrl);
    final result = await methodChannel.invokeMethod<String>('XSecondScreen',XJsonUtils.obj2Json(methodCommand));
    return result;
  }
  //刷新第二屏幕 分屏
  Future<dynamic> updateSecondScreen() async{
    XMethodCommand methodCommand=XMethodCommand('updateSecondScreen','');
    final result = await methodChannel.invokeMethod<String>('XSecondScreen',XJsonUtils.obj2Json(methodCommand));
    return result;
  }
  //发送消息到第二屏幕 分屏
  Future<dynamic> sendMsgToSecondScreen(String json) async{
    XMethodCommand methodCommand=XMethodCommand('sengMsgToSecondScreen',json);
    final result = await methodChannel.invokeMethod<String>('XSecondScreen',XJsonUtils.obj2Json(methodCommand));
    return result;
  }


  //4线 串口连接 商米机 String deviceType: SUNMI_T1,SUNMI_T2,SUNMI_T2s,rk3288
  Future<dynamic> serialPortConnect(String deviceType) async{
    XMethodCommand methodCommand=XMethodCommand('connect',deviceType);
    final result = await methodChannel.invokeMethod<String>('XSerialPort',XJsonUtils.obj2Json(methodCommand));
    return result;
  }


  //手动加载.so文件,适用android6.0, so文件名称(不包括.so后续),如: objectbox-jni
  Future<dynamic> manualSystemLoadLibrary(String soName) async{
    XMethodCommand methodCommand=XMethodCommand(soName,'');
    final result = await methodChannel.invokeMethod<String>('XManualSystemLoadLibrary',XJsonUtils.obj2Json(methodCommand));
    return result;
  }
  //Android 商米机
  Future<dynamic> initShangMiSDK() async{
    XMethodCommand methodCommand=XMethodCommand('init','');
    final result = await methodChannel.invokeMethod<String>('XShangMi',XJsonUtils.obj2Json(methodCommand));
    return result;
  }
  Future<dynamic> openDrawerByShangMi() async{
    XMethodCommand methodCommand=XMethodCommand('openDrawer','');
    final result = await methodChannel.invokeMethod<String>('XShangMi',XJsonUtils.obj2Json(methodCommand));
    return result;
  }
  Future<dynamic> cutPaperByShangMi() async{
    XMethodCommand methodCommand=XMethodCommand('cutPaper','');
    final result = await methodChannel.invokeMethod<String>('XShangMi',XJsonUtils.obj2Json(methodCommand));
    return result;
  }
  Future<dynamic> printMsgByShangMi(String msg) async{
    XMethodCommand methodCommand=XMethodCommand('printMsg',msg);
    final result = await methodChannel.invokeMethod<String>('XShangMi',XJsonUtils.obj2Json(methodCommand));
    return result;
  }
  Future<dynamic> printBytesByShangMi(List<int> bytes) async{
    //转json字符串
    String json=XJsonUtils.obj2Json(bytes);
    XMethodCommand methodCommand=XMethodCommand('printBytes',json);
    final result = await methodChannel.invokeMethod<String>('XShangMi',XJsonUtils.obj2Json(methodCommand));
    return result;
  }


  //返回系统桌面
  Future<dynamic> goBackToScreen() async{
    XMethodCommand methodCommand=XMethodCommand('goBackToScreen','');
    final result = await methodChannel.invokeMethod<String>('XAndroid',XJsonUtils.obj2Json(methodCommand));
    return result;
  }
  //安装apk权限
  Future<dynamic> checkInstallPermission() async{
    XMethodCommand methodCommand=XMethodCommand('checkInstallPermission','');
    final result = await methodChannel.invokeMethod<String>('XAndroid',XJsonUtils.obj2Json(methodCommand));
    return result;
  }
  //FOREGROUND_SERVICE权限
  Future<dynamic> checkFOREGROUND_SERVICEPermission() async{
    XMethodCommand methodCommand=XMethodCommand('check_FOREGROUND_SERVICE_Permission','');
    final result = await methodChannel.invokeMethod<String>('XAndroid',XJsonUtils.obj2Json(methodCommand));
    return result;
  }

  //发广播
  Future<dynamic> sendBroadcast(String json) async{
    XMethodCommand methodCommand=XMethodCommand('sendBroadcast',json);
    final result = await methodChannel.invokeMethod<String>('XAndroid',XJsonUtils.obj2Json(methodCommand));
    return result;
  }


}
